// 封装分类数据业务相关代码
import { ref } from 'vue'
import { useRoute } from 'vue-router';
import { getCategoryApi } from '@/api/category';
import { onBeforeRouteUpdate } from 'vue-router';

export function useCategory () {
  const categoryData = ref({})
  const route = useRoute()
  const getCategory = async (id = route.params.id  ) => {
  const res = await getCategoryApi(id)
  categoryData.value = res.result
  // console.log(categoryData.value)
 }
  getCategory()

  // 目标：路由参数变化的时候 可以把分类数据接口重新发送
  onBeforeRouteUpdate((to) => {
  // console.log('路由变化了')
  getCategory(to.params.id)
 }) 

  return {
    categoryData,
    getCategory
  }
}